package cn.rkylin.oms.base.batch.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenpineyu.fel.common.StringUtils;
import com.ruixue.oss.ServiceException;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.base.batch.service.ISkuBatchDetailService;
import cn.rkylin.oms.base.batch.vo.SkuBatchDetailVO;

@Controller
@RequestMapping("/batch")
public class SkuBatchDetailController extends ApolloController {
    private static final String BATCH_KIND_REAL = "real";
    private static final String BATCH_KIND_PLAN = "plan";

    private static final Log logger = LogFactory.getLog(SkuBatchDetailController.class);
    
    @Autowired
    ISkuBatchDetailService skuBatchDetaiService;
    
    @ResponseBody
    @RequestMapping(value = "/getBatchListBySourceId")
    public Map<String, Object> getBrandList(String sourceId,String kind) throws Exception {
        logger.info("根据来源查询批次效期：/batch/getBatchListBySourceId sourceId:'"+sourceId+"',kind:'"+kind+"'");
        if(StringUtils.isEmpty(sourceId)||StringUtils.isEmpty(kind)) {
            return getFailedMap("未提供有效的批次效期来源或者类型");
        }
        if(!BATCH_KIND_REAL.equals(kind)||!BATCH_KIND_PLAN.equals(kind)) {
            return getFailedMap("批次效期类型错误");
        }
        
        SkuBatchDetailVO params = new SkuBatchDetailVO();
        params.setSourceId(sourceId);
        params.setKind(kind);
        try {
            List<SkuBatchDetailVO> skuBatchDetailList = skuBatchDetaiService.querySkuBatchDetailList(params);
            Map<String, Object>  returnMap =  getSuccessMap("获取批次效期数据成功");
            returnMap.put(RETURN_DATA, skuBatchDetailList);
            return returnMap;
        }catch (ServiceException e) {
            logger.error(e.getMessage(), e);
            return getFailedMap("获取批次效期数据失败，请联系系统管理员");
        }
    }
    
    /**
     * getSuccessMap:获取成功消息Map. <br/>
     *
     * @author zhanghao
     * @param message
     * @return
     * @since JDK 1.6
     */
    private Map<String, Object> getSuccessMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, SUCCESS);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    };

    /**
     * getFailedMap:获取失败消息Map. <br/>
     *
     * @author zhanghao
     * @param message
     * @return
     * @since JDK 1.6
     */
    private Map<String, Object> getFailedMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, FAILED);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    };
}
